package com.test.service;

import com.test.dao.MyShowRepository;
import com.test.dt0.ShowEnum;
import com.test.dt0.ShowException;
import com.test.entity.MyShow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 服务 加工数据
 * Created by apple on 2017/5/12.
 */
@Service
public class MyShowService {
    @Autowired
    MyShowRepository myShowRepository;

    // 开启事务
    @Transactional
    public void inertTow() {
        MyShow show1 = new MyShow();
        show1.setName("i1");

        MyShow show2 = new MyShow();
        show2.setName("i2 i1ssdfsdfssdfsfsdfsdfsdfsdfdf");

        myShowRepository.save(show1);
        myShowRepository.save(show2);
    }

    // 主动抛出异常
    public MyShow execute(Integer id) throws Exception {
        MyShow myShow = myShowRepository.findOne(id);
        if (myShow.getAge() < 10) {
            throw new ShowException(ShowEnum.SCHOOL);
        } else if (myShow.getAge() < 16) {
            throw new ShowException(ShowEnum.MID);
        } else {
            return myShow;
        }
    }

    public MyShow findOne(Integer id) {
        return myShowRepository.findOne(id);
    }
}
